package com.projectsquirrel;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class NewObservation2 extends Activity {
	private static final int PREFERENCES_GROUP_ID = 11;
	private static final int PAGE_2 = 12;
	private static final int PAGE_3 = 13;
	private static final int PAGE_4 = 14;
	private static final int PAGE_5 = 15;
	private static final int PAGE_6 = 16;
	private static final int PAGE_7 = 17;
	
	private Context context;
	private LocationManager myManager;
	private LocListener Location_Listener;
	private boolean onEmulator;
	private TextView displayTime;
	private TextView displayDate;
	private Observation newObs;
	private EditText zipCode;
	private int mHour;
    private int mMinute;
    private int mYear;
    private int mMonth;
    private int mDay;
   // private int beforeL;
    static final int TIME_DIALOG_ID = 999990099;
    static final int DATE_DIALOG_ID = 999990098;
  private Button nextObsButton1;
    private SharedPreferences pref;



	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.observationformad);
	    ImageButton infoIconButton = (ImageButton) findViewById(R.id.infoScreenIcon);
	    ImageButton settingsIconButton = (ImageButton) findViewById(R.id.settingsScreenIcon);
	    //ImageButton nextObsButton1 = (ImageButton) findViewById(R.id.nextObsScreen1);
	    pref = getSharedPreferences("AppSettings",MODE_PRIVATE);
	    //onEmulator = getIntent().getExtras().getBoolean("OnEmulator");
	    onEmulator = pref.getBoolean("ON_EMULATOR", true);
	    Button nextObsButton1 = (Button) findViewById(R.id.nextObsScreen1);
	    //zipCode = (EditText) findViewById(R.id.confirmZIP);

	    //------Date and Time picker-------
	    Button mPickDate = (Button) findViewById(R.id.pickDate);
	    Button mPickTime = (Button) findViewById(R.id.pickTime); 
	    final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);
	    //-------End date and Time picker-----
	    	    

	    final TextView completitionStatus = (TextView) findViewById(R.id.textView2);
	    displayTime = (TextView) findViewById(R.id.timeDisplay);
	    displayDate = (TextView) findViewById(R.id.dateDisplay);
	   

	    context  = getApplicationContext();
	    final SharedPreferences pref = getSharedPreferences("AppSettings",MODE_PRIVATE);
	  	    
	    
	    //setup the gps manager
	    myManager = (LocationManager) getSystemService(LOCATION_SERVICE);	
	    //Location_Listener = new LocListener(myManager,this);
	    Location_Listener = LocListener.getInstance(myManager, this);
	    if(onEmulator){
	    	Location_Listener.startGPS();	    	
	    }
	    else{
	    	Location_Listener.startListeningNetwork();
	    }
	    
//	    beforeL = 0;
//		zipCode.addTextChangedListener(new TextWatcher(){
//			public void afterTextChanged(Editable s) {
//				
//				if(s.toString().length()>5){
//					zipCode.setText(zipCode .getText().toString().substring(0, beforeL));
//					
//				}
//				newObs.setZip(zipCode.toString());
//					
//			}
//		    public void beforeTextChanged(CharSequence s, int start, int count,
//		            int after) {
//		        // TODO Auto-generated method stub
//		    	beforeL = count;
//		    }
//		 
//		    public void onTextChanged(CharSequence s, int start, int before,
//		            int count) {
//		        // TODO Auto-generated method stub
//		 
//		    }
//		});
	    
	    //final Observation newObs = new Observation(pref.getString("userEmail", null),idNumberObs);
	    String email = pref.getString("userEmail", null);
	    //newObs = new Observation(pref.getString("userEmail", null));
	    newObs = Observation.getInstance(email);
	
	
		completitionStatus.setText("16% complete");

		
		
		// display time and date for the user
		displayTime.setText(Html.fromHtml("<i>" + newObs.getTime() + "</i>"));
		displayDate.setText(Html.fromHtml("<i>" + newObs.getDate() + "</i>"));
		

	    infoIconButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation2.this, infoMenu.class);
		        startActivity(i);
			}	    	
	    });
	    
	    settingsIconButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation2.this, settingsMenu.class);
		        startActivity(i);
			}	    	

	    });

	    
	    nextObsButton1.setOnClickListener(new OnClickListener() {
	    	public void onClick(View v) {
	    		Intent i = new Intent(NewObservation2.this, NewObservation3.class);
				//i.putExtra("OnEmulator", onEmulator);
		        startActivity(i);
		        //finish();
			}
	    });	    
	    	 // add a click listener to the button
        mPickDate.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
        		//Toast.makeText(getApplicationContext(), "PickDate!", 5000).show();
            }
        });
	  
        // add a click listener to the button
        mPickTime.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
        		//Toast.makeText(getApplicationContext(), "PickTime!", 5000).show();
            }
        });
     }
     	@Override
	protected void onStart(){
		super.onStart();
	}
	@Override
	protected void onDestroy() {
		
		super.onDestroy();
	}
	
	@Override
	protected void onPause() {
		
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
    // updates the date in the TextView
    private void updateDisplayDate() {
    	
    	newObs.setDate(mDay, mMonth, mYear);
    	displayDate.setText(newObs.getDate());

    }

    private void updateDisplayTime() {
    	newObs.setTime(mHour, mMinute);
    	displayTime.setText(newObs.getTime());               
    }
	
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case TIME_DIALOG_ID:
                return new TimePickerDialog(this,
                        mTimeSetListener, mHour, mMinute, false);
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                            mDateSetListener,
                            mYear, mMonth, mDay);
        }
        return null;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
            case TIME_DIALOG_ID:
                ((TimePickerDialog) dialog).updateTime(mHour, mMinute);
                break;
            case DATE_DIALOG_ID:
                ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
                break;
        }
    }  
    
    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    	public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
    		Calendar calendarTest = Calendar.getInstance();
    		int toastDisplayed = 0;
    		if(year<calendarTest.get(Calendar.YEAR)){
    			
    		}
    		else{
    		if((year>calendarTest.get(Calendar.YEAR)) || (year>=calendarTest.get(Calendar.YEAR) & monthOfYear>calendarTest.get(Calendar.MONTH)))
    		{
    			Toast.makeText(getApplicationContext(),"Warning: Date is in future!",Toast.LENGTH_LONG).show();
    			toastDisplayed = 1;
    		}
    		if((year>=Calendar.YEAR & monthOfYear>=calendarTest.get(Calendar.MONTH) & dayOfMonth>calendarTest.get(Calendar.DAY_OF_MONTH)))
    		{
    			if(toastDisplayed == 0)
    				Toast.makeText(getApplicationContext(),"Warning: Date is in future!",Toast.LENGTH_LONG).show();
    		}
    		}
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplayDate();
            }
    };

    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {

            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;
                updateDisplayTime();
            }
        };

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_MENU) ) {
	        
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
	super.onCreateOptionsMenu(menu);

	menu.add(PREFERENCES_GROUP_ID, PAGE_2, 0, "Date/Time")
	.setIcon(R.drawable.menuicondatetime);
	menu.add(PREFERENCES_GROUP_ID, PAGE_3, 0, "General")
	.setIcon(R.drawable.menuicongencom);
	menu.add(PREFERENCES_GROUP_ID, PAGE_4, 0, "Setting")
	.setIcon(R.drawable.menuiconsetting);
	menu.add(PREFERENCES_GROUP_ID, PAGE_5, 0, "Trees")
	.setIcon(R.drawable.menuicontree);
	menu.add(PREFERENCES_GROUP_ID, PAGE_6, 0, "Food")
	.setIcon(R.drawable.menuiconfood);
	menu.add(PREFERENCES_GROUP_ID, PAGE_7, 0, "Predators")
	.setIcon(R.drawable.menuiconpred);

	return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		Intent i;
		switch (item.getItemId()) {
		case PAGE_2:
			return true;
		case PAGE_3:
			i = new Intent(NewObservation2.this, NewObservation3.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_4:
			i = new Intent(NewObservation2.this, NewObservation4.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_5:
			i = new Intent(NewObservation2.this, NewObservation5.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_6:
			i = new Intent(NewObservation2.this, NewObservation6.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_7:
			i = new Intent(NewObservation2.this, NewObservation7.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		}
		return true;
	}
}
